package com.itheima;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class HashSetTest {
    public static void main(String[] args) {
        String arr[] = new String[100];
        ArrayList<String> tempList = new ArrayList<>();
        int count = 0,flag = 0;
        while (count<100){
            arr[count] = charRandom();
            System.out.print(arr[count]+" ");
            count++;
        }
        Set<String> hashSet = new HashSet<>();
        for(String st : arr){
            if(!hashSet.add(st)){
                flag++;
                tempList.add(st);
            }
        }
        System.out.println("重复的元素有"+flag+"个");
        System.out.println("分别是");
        System.out.println(tempList);
    }

    private static String charRandom() {
        String str = "";
        int count = 0;
        Random random = new Random();
        for(;;){
            char c = (char)(random.nextInt(90-65)+65);
            if(Character.isLetterOrDigit(c)){
                str+=c;
                count++;
                if(count==2){
                    break;
                }
            }
        }
        return str;
    }
}
